package config

import (
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"nodemessage.com/codetool/model"
)

func ConnectDb(code model.ConfGenCode) *gorm.DB {
	dbInfo := code.Db
	dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local",
		dbInfo.UserName, dbInfo.Password, dbInfo.Host, dbInfo.Port, dbInfo.DataBase)
	db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
	if err != nil {
		panic(err)
	}
	return db
}
